package com.winning.devops.batch.writer.ch05.config;

import com.winning.devops.batch.writer.ch05.domain.Customer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStreamWriter;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 多文件输出Job配置项
 * <p>
 * job/step
 *
 * @author chensj
 * @date 2020-03-07 18:30
 */
@Configuration
public class MultiFileItemWriterJobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    @Qualifier("multiFileItemDemoReader")
    private ItemReader<? extends Customer> multiFileItemDemoReader;
    /**
     * 全量输出到各个文件中
     */
    @Autowired
    @Qualifier("multiFileItemDemoWriter")
    private ItemWriter<? super Customer> multiFileItemDemoWriter;
    /**
     * 按照分类来输出到各个文件中
     */
    @Autowired
    @Qualifier("customerClassifierCompositeItemWriter")
    private ItemWriter<? super Customer> customerClassifierCompositeItemWriter;
    //=============下面两个在使用ClassifierCompositeItemWriter也是需要注入的,并且需要注入类型为ItemStreamWriter类型
    /**
     * 普通文件 writer
     */
    @Autowired
    @Qualifier("multiFlatFileDemoWriter")
    private ItemStreamWriter<? super Customer> multiFlatFileDemoWriter;
    /**
     * xml文件 writer
     */
    @Autowired
    @Qualifier("multiXmlFileItemDemoWriter")
    private ItemStreamWriter<? super Customer> multiXmlFileItemDemoWriter;

    @Bean
    public Job multiFileItemWriterDemoJob() {
        return jobBuilderFactory.get("multiFileItemWriterDemoJob")
                .start(multiFileItemWriterDemoStep())
                .build();
    }

    @Bean
    public Step multiFileItemWriterDemoStep() {
        return stepBuilderFactory
                .get("multiFileItemWriterDemoStep")
                .<Customer, Customer>chunk(10)
                .reader(multiFileItemDemoReader)
                //.writer(multiFileItemDemoWriter)
                .writer(customerClassifierCompositeItemWriter)
                .stream(multiFlatFileDemoWriter)
                .stream(multiXmlFileItemDemoWriter)
                .build();
    }
}
